#ifndef _HIDB_H_
#define _HIDB_H_

#include <tcrdb.h>
#include "cfg.h"

#ifdef __cplusplus
extern "C"{
#endif

#define HIDB_VNODE_MAX	16
#define HIDB_HOST_SIZE	32

//hidb type
#define HIDB_META	1
#define HIDB_DATA	2

//hidb STAT
#define HIDB_NULL	0
#define HIDB_FRESH	1
#define HIDB_OK		2
#define HIDB_ERR	3

#define HIDB_DEF_INTV	(60*60*24)		/* one day */
#define HIDB_DEF_SCORE	(0)

#define HASHVND(str, len, group)	\
	((group).vnodes + dohash((str), (len)) % (group).vnum)

//metadb fileds
enum HI_FILED {
	/* URL is key */
	F_STAT	= 0,	/* value - Status */		
	F_SIGN	= 1,	/* value - Signature */	
	F_MTIME	= 2,	/* value - ModifedTime */	
	F_FTIME	= 3,	/* value - FetchTime */	
	F_FINTV	= 4,	/* value - FetchInterval */
	F_SCORE	= 5,	/* value - Score */		
};

#define HIDB_URL_SIZE	1024
#define HIDB_SIGN_SIZE	33
#define HIDB_VAL_SIZE	32

#define FVAL_INIT		{.stat = HIDB_NULL, .sign = {0}}
typedef struct FVAL {
	int			stat;
	time_t		mtime;
	time_t		ftime;
	int			fintv;
	int			score;
	char		sign[HIDB_SIGN_SIZE];
} FVAL;

typedef struct VNODE {
	char		host[HIDB_HOST_SIZE];
	int			port;
	int			type;
	TCRDB		*dbp;
} VNODE;

typedef struct VGROUP {
	int			vnum;
	VNODE		vnodes[HIDB_VNODE_MAX];
} VGROUP;

typedef struct HIDB {
	VGROUP		metadb;
	VGROUP		datadb;
} HIDB;

int hidb_free(HIDB * p);
HIDB * hidb_new(int vnum, VNODE * vnodes);
int hidb_put(HIDB * p, const char * url, const char * text, int textlen);
char * hidb_getbysign(const HIDB * p, const char * sign, int * datalenp);
char * hidb_getbyurl(const HIDB * p, const char * url, int * datalenp);
TCLIST * hidb_qry(HIDB * p, enum HI_FILED f, const char * start, const char * end, int limit);

#ifdef __cplusplus
}
#endif

#endif
